//
//  SearchResultViewController.m
//  SearchBarTest
//
//  Created by 蓝布鲁 on 2017/3/21.
//  Copyright © 2017年 蓝布鲁. All rights reserved.
//

#import "SearchResultViewController.h"
#import "SearchModel.h"
#import "TeacherInfoViewController.h"
#import "ActivityInfoViewController.h"
#import "SearchViewController.h"

@interface SearchResultViewController ()<UITableViewDelegate,UITableViewDataSource>



@end

@implementation SearchResultViewController

#pragma mark - 系统回调方法
- (void)viewDidLoad {
    [super viewDidLoad];
    // 1.初始化tableView
    [self setupTableView];
}

#pragma mark - 自定义方法
- (void)setupTableView {
    // SearchBar有坑,坐标会向下移动
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -40, SCREEN_WIDTH, SCREEN_HEIGHT)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:self.tableView];
}

#pragma mark - tableView回调方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.searchResult.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELLID"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELLID"];
    }

    // 取出model
    SearchModel *model = self.searchResult[indexPath.row];
    cell.textLabel.text = model.name;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:true];

    self.searchViewController.searchController.active = false;

    // 取出搜索模型
    SearchModel *model = self.searchResult[indexPath.row];
    if(model.type == 1) {
        // 跳转导师详情
        TeacherInfoViewController *controller = [[TeacherInfoViewController alloc] init];
        controller.teacherID = model.search_id;
        [self.searchViewController.navigationController pushViewController:controller animated:true];
    } else {
        // 跳转活动详情
        ActivityInfoViewController *controller = [[ActivityInfoViewController alloc] init];
        controller.activityID = [model.search_id intValue];
        [self.searchViewController.navigationController pushViewController:controller animated:true];
    }
}


@end
